<template>
    <div class="wrap">
        <div class="top">
            <div class="hosname" @click="$router.push(`/hospital/detail?hoscode=${$route.query.hoscode}`)">{{
                workData.baseMap?.hosname }}</div>
            <div class="line">|</div>
            <div>{{ workData.baseMap?.bigname }}</div>
            <div class="dot">.</div>
            <div class="department">{{ workData.baseMap?.depname }}</div>
        </div>
        <div class="center">
            <h1>{{ workData.baseMap?.workDateString }}</h1>
            <div class="container">
                <div class="items" @click="changeItem(item)" :class="{
                    active: item.status == -1 || item.availableNumber == -1,
                    cur: item.workDate == workTime.workDate
                }" v-for="item in workData.bookingScheduleList">
                    <div class="tops">{{ item.workDate }}
                        <span>{{ item.dayOfWeek }}</span>
                    </div>
                    <div class="bottom">
                        <div v-if="item.status == -1">停止挂号</div>
                        <div v-if="item.status == 1">即将放号</div>
                        <div v-if="item.status == 0">
                            {{ item.availableNumber == -1 ? '已约满' : `有号` }}
                        </div>
                    </div>
                </div>
            </div>
            <el-pagination v-model:current-page="hospitalWorkParams.page"
                @current-change="getWorkData(hospitalWorkParams)" layout="prev, pager, next" :total="total" />
        </div>
        <div class="bottom1">
            <div class="doctor" v-if="workTime.status !== 1">
                <div class="moring">
                    <div class="list-title">
                        上午号源
                    </div>
                    <div class="doc_info" v-for="item in moringData" :key="item.id">
                        <div class="left">
                            <div class="top_wrapper">
                                <div class="title">{{ item.title }}</div>
                                <div class="split"></div>
                                <div class="name">{{ item.docname }}</div>
                            </div>
                            <div class="desc">{{ item.skill }}</div>
                        </div>
                        <div class="right">
                            <div class="fee">￥{{ item.amount }}</div>
                            <el-button :disabled="flag" class="syu"
                                :class="{ active1: item.status == -1 || item.availableNumber == -1 }" type="primary"
                                size="large" @click="goStep2(item)">剩余 {{ item.availableNumber
                                }}</el-button>
                        </div>
                    </div>
                </div>
                <div class="moring">
                    <div class="list-title">
                        下午号源
                    </div>
                    <div class="doc_info" v-for="item in afterData" :key="item.id">
                        <div class="left">
                            <div class="top_wrapper">
                                <div class="title">{{ item.title }}</div>
                                <div class="split"></div>
                                <div class="name">{{ item.docname }}</div>
                            </div>
                            <div class="desc">{{ item.skill }}</div>
                        </div>
                        <div class="right">
                            <div class="fee">￥{{ item.amount }}</div>
                            <el-button :disabled="flag" type="primary" size="large" @click="goStep2(item)">剩余 {{
                                item.availableNumber
                                }}</el-button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="will" v-else>
                <div>
                    明天<span>8.20</span>放号
                </div>
                <div class="daojishi">倒计时：<span class="time">{{ timeDifference }}</span></div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getDoctorWorkAPI, getHospitalRegisterAPI } from '@/api/hospital';
import { BookingSchedule, HospitalDoctorArr, HospitalDoctorInfo, HospitalDoctorParams, HospitalDoctorResponseData, HospitalWorkParams, HospitalWorkResponseData } from '@/api/hospital/type';
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

let timeDifference = ref('') // 存储计算出的时差
let timer = ref()
let workData = ref<any>({})
let workTime = ref<BookingSchedule>({
    workDate: '',
    workDateMd: '',
    dayOfWeek: '',
    docCount: 0,
    reservedNumber: 0,
    availableNumber: 0,
    status: 0,
})
let doctorData = ref<HospitalDoctorArr>([])
let total = ref<number>(0)
let $route = useRoute()
let flag = ref<boolean>(false)
let hospitalWorkParams = reactive<HospitalWorkParams>({
    page: 1,
    limit: 5,
    hoscode: $route.query.hoscode as string,
    depcode: $route.query.depcode as string
})

let hospitalDoctorParams = reactive<HospitalDoctorParams>({
    hoscode: $route.query.hoscode as string,
    depcode: $route.query.depcode as string,
    workDate: ''
})

onMounted(async () => {
    await getWorkData(hospitalWorkParams)
    await getDoctorData()
    calculateTimeDifference()
})
onUnmounted(() => {
    clearInterval(timer.value)
})

function calculateTimeDifference() {
    timer.value = setInterval(() => {
        // 获取当前时间
        const now: any = new Date();
        // 设置目标时间为明天的8:20
        const target: any = new Date();
        target.setHours(8, 20, 0, 0); // 设置小时、分钟、秒、毫秒
        // 如果当前时间已经过了今天的8:20，则目标时间设为后天的8:20
        if (now > target && now.getHours() < 24 - 8) { // 这里假设不会跨越两天以上的时间差，即只考虑今天和明天
            target.setDate(target.getDate() + 1);
        }
        // 计算时间差（毫秒）
        const diffInMs = target - now;
        // 转换为小时、分钟、秒
        const diffInSecs = Math.floor(diffInMs / 1000);
        const hours = Math.floor(diffInSecs / 3600);
        const minutes = Math.floor((diffInSecs % 3600) / 60);
        const seconds = diffInSecs % 60;
        // 格式化时间差
        timeDifference.value = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    }, 1000)
}

const getDoctorData = async () => {
    let res: HospitalDoctorResponseData = await getDoctorWorkAPI(hospitalDoctorParams)
    if (res.code == 200) {
        doctorData.value = res.data
    }
}
let moringData = computed(() => {
    return doctorData.value.filter((doc: HospitalDoctorInfo) => {
        return doc.workTime == 0
    })
})
let afterData = computed(() => {
    return doctorData.value.filter((doc: HospitalDoctorInfo) => {
        return doc.workTime == 1
    })
})
const changeItem = (item: any) => {
    flag.value = false
    workTime.value = item
    if (item.status == -1 || item.availableNumber == -1) {
        flag.value = true
    }
    hospitalDoctorParams.workDate = item.workDate
    getDoctorData()
}
const getWorkData = async (hospitalWorkParams: HospitalWorkParams) => {
    let { page, limit, hoscode, depcode } = hospitalWorkParams
    let res: HospitalWorkResponseData = await getHospitalRegisterAPI(page, limit, hoscode, depcode)
    workData.value = res.data
    total.value = res.data.total
    hospitalDoctorParams.workDate = workData.value.bookingScheduleList[0].workDate
    workTime.value = workData.value.bookingScheduleList[0]
    if (workData.value.bookingScheduleList[0].status == -1 || workData.value.bookingScheduleList[0].availableNumber == -1) {
        flag.value = true
    }
}

let $router = useRouter()
const goStep2 = (doc: HospitalDoctorInfo) => {
    $router.push(`/hospital/register_step2?scheduleId=${doc.id}`)
}
</script>

<style scoped lang="scss">
.wrap {
    .top {
        display: flex;
        color: #7f7f7f;

        .hosname {
            cursor: pointer;
        }

        .line {
            margin: 0 10px;
            color: #eee;
        }

        .dot {
            margin: 0 5px;
            align-items: center;
        }

        .department {
            font-weight: bold;
        }
    }

    .center {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        h1 {
            margin: 50px 0 30px 0;
        }

        .container {
            display: flex;
            width: 100%;

            .items {
                flex: 1;
                border: 1px solid #e8f2ff;
                color: #4990f1;
                cursor: pointer;
                margin-right: 16px;
                display: flex;
                flex-direction: column;
                align-items: center;
                margin-bottom: 20px;

                &.active {
                    color: #7f7f7f;

                    .tops {
                        background-color: #d0cece;
                    }
                }

                &.cur {
                    border: 1px solid #4990f1;
                    font-weight: bold;
                    transition: all .2s;
                    transform: scale(1.03);
                }

                .tops {
                    background-color: #e8f2ff;
                    width: 100%;
                    line-height: 30px;
                    text-align: center;
                    white-space: nowrap;
                    box-sizing: border-box;
                    padding: 5px;
                    font-size: 14px;

                    span {
                        margin-left: 6px;
                        font-size: 12px;
                    }
                }

                .bottom {
                    line-height: 45px;
                    font-size: 15px;
                }
            }
        }
    }

    .bottom1 {
        margin-top: 60px;

        .will {
            font-size: 16px;
            color: #333;
            text-align: center;
            font-weight: 400;
            display: flex;
            align-items: center;
            flex-direction: column;

            span {
                color: #4990f1;
                margin: 0 5px;
                display: inline-block;
            }

            .daojishi {
                display: flex;
                align-items: center;
                color: #999;
                font-size: 16px;
                margin-top: 20px;
                text-align: center;

                .time {
                    margin-left: 14px;
                    font-weight: 700;
                    font-size: 30px;
                    letter-spacing: 1px;
                    color: #4990f1;
                }
            }
        }

        .doctor {

            .moring {
                .list-title {
                    display: flex;
                    align-items: center;
                    letter-spacing: 1px;
                    color: #999;

                    &::before {
                        display: inline-block;
                        content: '';
                        height: 14px;
                        width: 4px;
                        background: #4990f1;
                        margin-right: 6px;
                        border-radius: 2px;
                    }
                }

                .doc_info {
                    display: flex;
                    justify-content: space-between;
                    margin: 40px 0;

                    .left {
                        display: flex;
                        flex-direction: column;

                        .desc {
                            margin-top: 10px;
                            letter-spacing: 1px;
                            color: #999;
                        }

                        .top_wrapper {
                            display: flex;
                            margin-bottom: 20px;

                            .title {
                                letter-spacing: 1px;
                                font-weight: 700;
                                color: #333;
                                font-size: 14px;
                            }

                            .split {
                                border-left: 1px solid #eee;
                                width: 1px;
                                height: 16px;
                                margin: 0 10px;
                            }
                        }
                    }

                    .right {
                        display: flex;
                        align-items: center;

                        .fee {
                            letter-spacing: 1px;
                            color: #4990f1;
                            font-weight: 700;
                            margin-right: 20px;
                        }

                        .syu {

                            &.active1 {
                                background-color: #999;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>